import asyncio, random, pyautogui, os, time
from db import RolesDB

from core.base_controller import BaseController
from core.utils import focus_wow
from controllers.login_role_controller import LoginRoleController
from controllers.common_actions import CommonActions
from controllers.auto_order.auto_order_actions import AutoOrderActions
from controllers.auto_craft.auto_craft_actions import AutoCraftActions
from controllers.auto_salvage_card.auto_salvage_card_actions import AutoSalvageCardActions

from controllers.record_events.record_player import ActionPlayer

class CraftGroupController(BaseController):
  def __init__(self, task_list, ws_client):
    super().__init__(task_list, ws_client)
    self.toggle_to_order_tab = False
    self.common_actions = None
    self.auto_order_actions = None
    self.auto_craft_actions = None
    self.auto_salvage_card_actions = None
    self.need_auto_mission_order = False
    self.need_auto_craft = False
    self.need_auto_order = False
    self.need_create_dn_object = False

  async def extra_actions(self, log_prefix):
    self.common_actions = CommonActions(log_prefix=log_prefix, ws_client=self.ws_client)
    self.auto_order_actions = AutoOrderActions(log_prefix=log_prefix, ws_client=self.ws_client)
    self.auto_craft_actions = AutoCraftActions(log_prefix=log_prefix, ws_client=self.ws_client)
    self.auto_salvage_card_actions = AutoSalvageCardActions(log_prefix=log_prefix, ws_client=self.ws_client)
    
    await self.log("extra_actions start", log_prefix=log_prefix)
    
    if self.extra_data.get("reset_keyboard", 0) == 1:
      ## 重置按键
      await self.common_actions.reset_keymap()
    
    if self.extra_data.get("get_emails", 0) == 1:
      ## 收邮件
      await self.common_actions.get_emails()
      
    if self.extra_data.get("salvage_card", 0) == 1:
      ## 拆卡牌
      await self.auto_salvage_card_actions.start_salvage()
      
    profession_type = self.extra_data.get("profession_type", 0)
    craft_dz = profession_type == 0 or profession_type == 1
    craft_fm = profession_type == 0 or profession_type == 2
    self.need_auto_mission_order = self.extra_data.get("auto_mission_order", 0) == 1
    self.need_auto_run_record_events = self.extra_data.get("auto_run_record_events", 0) == 1
    self.need_auto_craft = self.extra_data.get("auto_craft", 0) == 1
    self.need_auto_order = self.extra_data.get("auto_order", 0) == 1
    self.need_create_dn_object = self.extra_data.get("create_dn_object", 0) == 1
    
    if self.need_auto_mission_order:
      await self.do_mission_order_actions(log_prefix=log_prefix)
      await self.common_actions.screenshot(self.cur_role.role_name)
    if self.need_auto_run_record_events:
      await self.do_run_record_events_actions(log_prefix=log_prefix)
      await self.common_actions.screenshot(self.cur_role.role_name)
      
    if not self.need_auto_craft and not self.need_auto_order:
      return
    
    self.toggle_to_order_tab = False if self.need_auto_order else True
    if random.random() > 0.5:
      if craft_dz:
        await self.do_dz_actions()
        pyautogui.press("esc")
      if craft_fm:
        await self.do_fm_actions()
    else:
      if craft_fm:
        await self.do_fm_actions()
        pyautogui.press("esc")
      if craft_dz:
        await self.do_dz_actions()
        
    # if self.need_auto_craft:
    await self.common_actions.send_role_bag_objects(only_send_craft_regents=not self.need_auto_order)
    await self.log("extra_actions end", log_prefix=log_prefix)

  async def do_run_record_events_actions(self, log_prefix):
    await self.log("播放录制 start", log_prefix=log_prefix)
    record_file = self.extra_data.get("record_events_file", "abs_1753415202.json")
    events_dir = os.getenv("EVENTS_DIR")
    await ActionPlayer().start(os.path.join(events_dir, record_file))
    await self.log("播放录制 end", log_prefix=log_prefix)
    await asyncio.sleep(2)
    
  async def do_mission_order_actions(self, log_prefix):
    await self.log("盟约收菜 start", log_prefix=log_prefix)
    await asyncio.sleep(random.uniform(3, 4))
    await self.log("盟约收菜 交互", log_prefix=log_prefix)
    pyautogui.press("9")
    await asyncio.sleep(random.uniform(2, 3))
    await self.log("盟约收菜 完成订单", log_prefix=log_prefix)
    pyautogui.press("c" if random.random() > 0.5 else "f7")
    await asyncio.sleep(random.uniform(3, 4))
    await self.log("盟约收菜 开始订单", log_prefix=log_prefix)
    pyautogui.press("b" if random.random() > 0.5 else "f5")
    await asyncio.sleep(random.uniform(6, 7))
    await self.log("盟约收菜 end", log_prefix=log_prefix)
    
  
  async def do_dz_actions(self):
    ## 切换到锻造桌
    if self.cur_role.craft_type == 1:
      await self.common_actions.toggle_dz_table()
    else:
      pyautogui.press("num0")
      await asyncio.sleep(random.uniform(1, 2))
    ## 打开锻造界面
    await self.common_actions.open_dz_frame()
    
    if self.need_create_dn_object:
      await self.common_actions.create_dh_object()
    
    ## 检查buff
    await self.common_actions.check_buffs(dh_buff=True, qs_buff=True, sl_buff=False)
    
    if not self.toggle_to_order_tab:
      await self.common_actions.toggle_to_order_tab()
      self.toggle_to_order_tab = True
    if self.need_auto_order:
      ## 开始锻造订单
      await self.auto_order_actions.start_dz_order()
    if self.need_auto_craft:
      ## 开始制作锻造物品
      await self.auto_craft_actions.start_dz_craft()
    
    
  async def do_fm_actions(self):
    ## 切换到附魔桌
    if self.cur_role.craft_type == 1:
      await self.common_actions.toggle_fm_table()
    ## 打开附魔界面
    await self.common_actions.open_fm_frame()
    
    ## 检查buff
    await self.common_actions.check_buffs(dh_buff=False, qs_buff=True, sl_buff=True)
    
    if not self.toggle_to_order_tab:
      await self.common_actions.toggle_to_order_tab()
      self.toggle_to_order_tab = True
    
    if self.need_auto_order:
      ## 开始附魔订单
      await self.auto_order_actions.start_fm_order()
      
    if self.need_auto_craft:
      ## 设置配方
      await self.auto_craft_actions.set_cur_fm_recipe()
      ## 开始制作锻造物品
      await self.auto_craft_actions.start_fm_craft()